﻿using Microsoft.AspNetCore.Http;
using RsCode;
using RsCode.Domain;

namespace RsCms.Core.Tags
{
    public class TagsDomainService:ITagsDomainService
    {
        IRepository<Entities.Tags> tagsRepositorty;
        IHttpContextAccessor httpContextAccessor;
        public TagsDomainService(IRepository<Entities.Tags> repository,
            IHttpContextAccessor httpContextAccessor)
        {
            tagsRepositorty = repository;
            this.httpContextAccessor= httpContextAccessor;
        }
        string LoginUserId
        {
            get
            {
                var claims = httpContextAccessor?.HttpContext?.User.Claims;
                return claims?.First(x => x.Type == "UserId").Value ?? "";
            }
        }
        public async Task<PageData<Entities.Tags>> PageAsync(int page, int pageSize)
        {
            return await tagsRepositorty.PageAsync(page,pageSize); ;
        }

        public  Task<Entities.Tags> SaveAsync(Entities.Tags tags)
        {
            if(string.IsNullOrWhiteSpace(tags.Id))
            {
                tags.Id = Guid.NewGuid().ToString("N");
                tags.CreatedAt = DateTime.Now;
                tags.Visibility = "public";
            }else
            {
                tags.UpdatedAt = DateTime.Now;
            }
            var info=tagsRepositorty.InsertOrUpdate(tags);
            return Task.FromResult( info);
        }
    }
}
